Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C# / VB.NET - [VB 2010] Esportare dati da db Paradox a Excel versione 2003
Forum - C# / VB.NET - [VB 2010] Esportare dati da db Paradox a Excel versione 2003

Avatar
vankraster (Member)
Rookie


Messaggi: 32
Iscritto: 05/11/2010

Segnala al moderatore
Postato alle 12:55
Sabato, 13/08/2011
Io avrei la necessità di esportare dati da un db Paradox a un file Excel versione 2003, il problema è che ci sono riuscito ma siccome il db paradox ne ha 50.000 records e 15 campi per l'esportazione ci vogliono 3 ore. Tutto risolto tramite codice da vb 2010. Prima spiego il concetto che ho usato poi mettero il codice:
Apro il db Paradox lo interrogo con la query, apro il file excel e poi dal risultato  per ogni riga inserisco nel file excel i vari campi. Quello che vi chiedo a voi e se posso migliorare il programma in modo che l'esportazione mi duri al massimo 1 ora.

Codice sorgente - presumibilmente VB.NET

  1. Dim sExcelPath, sdbPath, sLog As String
  2.         sExcelPath = System.IO.Path.GetDirectoryName(Application.ExecutablePath)
  3.         sdbPath = "C:\db_From"
  4.         sLog = sExcelPath & "\logs"
  5.  
  6.         Dim sFIle As System.IO.StreamWriter
  7.  
  8.  
  9.         sFIle = System.IO.File.CreateText(sLog & "\" & Date.Now.DayOfYear & ".txt")
  10.  
  11.         ' Add some information to the file.
  12.         sFIle.WriteLine(DateTime.Now.TimeOfDay.ToString & "Creazione del db")
  13.  
  14.         Dim tblmy As DataTable
  15.         Dim oledbParadoxconn As New OleDb.OleDbConnection
  16.         'EXCEL######################################
  17.         Dim xlApp As Excel.Application
  18.         Dim xlWorkBook As Excel.Workbook
  19.         Dim xlWorkSheet As Excel.Worksheet
  20.         'variabili memmorizazione dalla query#############################################
  21.         Dim ShopCOde, DelDate, StockCode, ShrtDesc, WEBDESC, BRAND, VarDesc, Varcode, VarAltCode, PurchCost, Purchvat, UnitPrice, PriceVat, Desc As String
  22.         Dim DescFrom, DescTo, AdoCOst1, AdoCost2, InvtryDate, Stock, Gruppo, sGruppo, TMisura, GMisura, Misura, Stagione, Anno, Sesso, Colore, Materiale, Box, PICFile As String
  23.  
  24.  
  25.         'COSTANTI#####################################
  26.         ShopCOde = "011"
  27.         DelDate = DateTime.Now.Date
  28.  
  29.  
  30.  
  31.         Try
  32.             sFIle.WriteLine(DateTime.Now.TimeOfDay.ToString & "Inizio query db paradox")
  33.             lblProc.Text = "Query database Paradox"
  34.             Dim Sqltext As String = "SELECT A.[Articolo Fornitore], A.Descrizione, A.MARCA, A.[ARTICOLO ETICHETTA], A.[CATEGORIA MERCEOLOGICA], A.[TIPO ARTICOLO], V.[DENOMINAZIONE VARIANTE 1], L.[PREZZO IVATO E], G.[DATA ULTIMO MOVIMENTO], G.[QTA INIZIALE]-G.[QTA SCARICO]+G.[QTA CARICO] AS TOT,U.UBICAZIONE, V.[VARIANTE 2], V.[VARIANTE 1] FROM ARTICOLI A, ARTBASE V, UBICAZIONE U, GIACENZA G,LISTINO L WHERE V.[ARTICOLO INTERNO]=A.[ARTICOLO] AND  A.[UBICAZIONE]=U.[CODICE]  AND G.[ARTICOLO]=V.[ARTICOLO ETICHETTA] AND L.[ARTICOLO]=A.[ARTICOLO]"
  35.             Dim oledbconnstr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sdbPath & ";Extended Properties=Paradox 5.x;"
  36.             oledbParadoxconn.ConnectionString = oledbconnstr
  37.             oledbParadoxconn.Open()
  38.  
  39.             Dim myDataAdapter As New OleDb.OleDbDataAdapter(Sqltext, oledbParadoxconn)
  40.             Dim dtgetdata As New DataSet
  41.  
  42.  
  43.             myDataAdapter.Fill(dtgetdata, "aaaa")
  44.  
  45.             '   Me.dataGrid1.DataSource = dtgetdata
  46.             '   Me.dataGrid1.DataMember = "aaaa"
  47.             tblmy = dtgetdata.Tables("aaaa")
  48.  
  49.             '#####COPIARE DALLA COPIA AL NUOVO
  50.             'backupXLS.xls -copia
  51.             'fogl.xls = nuovo
  52.             'cancella file se esiste già
  53.             MsgBox("fine query")
  54.             sFIle.WriteLine(DateTime.Now.TimeOfDay.ToString & "fine della query")
  55.  
  56.             If System.IO.File.Exists(sExcelPath & "\fogl.xls") Then System.IO.File.Delete(sExcelPath & "\fogl.xls")
  57.  
  58.             System.IO.File.Copy(sExcelPath & "\backupXLS.xls", sExcelPath & "\fogl.xls")
  59.  
  60.             'EXCEL ##################
  61.             xlApp = New Excel.Application
  62.             xlWorkBook = xlApp.Workbooks.Open(sExcelPath & "\fogl.xls")
  63.             xlWorkSheet = xlWorkBook.Worksheets("Foglio1")
  64.             MsgBox("fine copia xls")
  65.  
  66.  
  67.             'Progress bar###########################################
  68.             Progress.Maximum = tblmy.Rows.Count
  69.             Dim i As Integer = 0
  70.             lblProc.Text = "Inserimento nel file di EXCEL i dati"
  71.             sFIle.WriteLine(DateTime.Now.TimeOfDay.ToString & "fine della copia del file excel - inizio della scrittura nel file excel")
  72.             WEBDESC = ""
  73.             PurchCost = "0"
  74.             Purchvat = "0"
  75.             PriceVat = "20"
  76.             Desc = "0"
  77.             DescFrom = ""
  78.             DescTo = ""
  79.             AdoCOst1 = "0"
  80.             AdoCost2 = "0"
  81.             Materiale = ""
  82.             Box = ""
  83.             PICFile = ""
  84.  
  85.             MsgBox(tblmy.Rows.Count)
  86.  
  87.             For Each x In tblmy.Rows
  88.                 StockCode = x("Articolo Fornitore")
  89.                 ShrtDesc = x("Descrizione")
  90.                 BRAND = x("Marca")
  91.                 VarDesc = ShrtDesc & " " & x("Variante 2")
  92.                 Varcode = StockCode & "." & x("Variante 2")
  93.                 VarAltCode = x("Articolo etichetta")
  94.                 UnitPrice = x("PREZZO IVATO E")
  95.                 InvtryDate = x("DATA ULTIMO MOVIMENTO")
  96.                 Stock = x("TOT")
  97.                 Gruppo = x("CATEGORIA MERCEOLOGICA")
  98.                 sGruppo = x("Tipo Articolo")
  99.                 If IsDBNull(x("Denominazione Variante 1")) Then
  100.                     TMisura = ""
  101.                 Else
  102.                     TMisura = x("Denominazione Variante 1")
  103.                 End If
  104.                 GMisura = x("TIPO ARTICOLO")
  105.                 If IsDBNull(x("Variante 1")) Then
  106.                     Misura = "UNICA"
  107.                 Else
  108.                     Misura = x("Variante 1")
  109.                 End If
  110.                 Stagione = x("UBICAZIONE")
  111.                 Anno = Stagione
  112.                 Sesso = Gruppo
  113.                 If IsDBNull(x("Variante 2")) Then
  114.                     Colore = "."
  115.                 Else
  116.                     Colore = x("Variante 2").ToString
  117.                 End If
  118.  
  119.                 'SCrivi sul foglio Excel
  120.                 xlWorkSheet.Cells(i + 2, 1).value = ShopCOde
  121.                 xlWorkSheet.Cells(i + 2, 2).value = DelDate
  122.                 xlWorkSheet.Cells(i + 2, 3).value = StockCode
  123.                 xlWorkSheet.Cells(i + 2, 4).value = ShrtDesc
  124.                 xlWorkSheet.Cells(i + 2, 5).value = WEBDESC
  125.                 xlWorkSheet.Cells(i + 2, 6).value = BRAND
  126.                 xlWorkSheet.Cells(i + 2, 7).value = VarDesc
  127.                 xlWorkSheet.Cells(i + 2, 8).value = Varcode
  128.                 xlWorkSheet.Cells(i + 2, 9).value = VarAltCode
  129.                 xlWorkSheet.Cells(i + 2, 10).value = PurchCost
  130.                 xlWorkSheet.Cells(i + 2, 11).value = Purchvat
  131.                 xlWorkSheet.Cells(i + 2, 12).value = UnitPrice
  132.                 xlWorkSheet.Cells(i + 2, 13).value = PriceVat
  133.                 xlWorkSheet.Cells(i + 2, 14).value = Desc
  134.                 xlWorkSheet.Cells(i + 2, 15).value = DescFrom
  135.                 xlWorkSheet.Cells(i + 2, 16).value = DescTo
  136.                 xlWorkSheet.Cells(i + 2, 17).value = AdoCOst1
  137.                 xlWorkSheet.Cells(i + 2, 18).value = AdoCost2
  138.                 xlWorkSheet.Cells(i + 2, 19).value = InvtryDate
  139.                 xlWorkSheet.Cells(i + 2, 20).value = Stock
  140.                 xlWorkSheet.Cells(i + 2, 21).value = Gruppo
  141.                 xlWorkSheet.Cells(i + 2, 22).value = sGruppo
  142.                 xlWorkSheet.Cells(i + 2, 23).value = TMisura
  143.                 xlWorkSheet.Cells(i + 2, 24).value = GMisura
  144.                 xlWorkSheet.Cells(i + 2, 25).value = Misura
  145.                 xlWorkSheet.Cells(i + 2, 26).value = Stagione
  146.                 xlWorkSheet.Cells(i + 2, 27).value = Anno
  147.                 xlWorkSheet.Cells(i + 2, 28).value = Sesso
  148.                 xlWorkSheet.Cells(i + 2, 29).value = Colore
  149.                 xlWorkSheet.Cells(i + 2, 30).value = Materiale
  150.                 xlWorkSheet.Cells(i + 2, 31).value = Box
  151.                 xlWorkSheet.Cells(i + 2, 32).value = PICFile
  152.  
  153.                 'indicazione
  154.                 Progress.Value = i
  155.                 i += 1
  156.             Next
  157.             sFIle.WriteLine(DateTime.Now.TimeOfDay.ToString & "fine della scrittura file excel")
  158.  
  159.             'chiudi foglio EXCEL#####################################
  160.             xlWorkBook.Close()
  161.             xlApp.Quit()
  162.  
  163.             releaseObject(xlApp)
  164.             releaseObject(xlWorkBook)
  165.             releaseObject(xlWorkSheet)
  166.             lblProc.Text = "Fine Inserimento - Inizion trasfero dati FTP"


Ultima modifica effettuata da Il Totem il 14/08/2011 alle 12:07
PM
Avatar
Il Totem (Admin)
Guru^2


Messaggi: 3635
Iscritto: 24/01/2006

Up
0
Down
V
Segnala al moderatore
Postato alle 12:14
Domenica, 14/08/2011
50k record non sono tanti e mi sembra assurdo che ti ci vogliano tre ore. Mi sarebbe parso strano anche se ti avesse preso più di 10 minuti.
Per iniziare puoi usare un DataReader al posto del DataSet. E' molto meglio se leggi un risultato e lo scrivi piuttosto che leggerli tutti e scriverli tutti in blocco.
http://msdn.microsoft.com/it-it/library/system.data.oledb. ...
Puoi anche evitare di dichiarare tutte quelle variabili usando semplicemente degli indici per riferirti a un certo attributo di un record.

PM